var collectVue = new Vue({
	el: "#app",
	data: {
		active: true,
		type: "项目",
		empty: true,
		page: 1,
		listType: 'project',
		isExcute: false,
		isBottom: false,
		count: 0,
		products: [],
		projects: [],
		imgUrl: "https://app.meijiangzi.com/static/mini_",
		startX: 0,
		disX: 0,
		picWith:750

	},
	methods: {
		handleScroll: function() {
			// 防止重复
			if (this.isExcute) {
				return;
			}
			if (this.isBottom) {
				return;
			}
			var flag = srollEvent.isReachBottom();
			if (!flag) {
				return;
			}
			this.isExcute = true;
			this.page++;
			this.show(listType);
		},
		show: function(type) {
			this.listType = type;
			this.page = 1;
			this.isBottom = false;
			this.isExcute = false;
			if (type === 'product') {
				this.products = [];
				this.active = false;
				this.type = "商品";
				wx.showLoading({
					title: "加载中..."
				});
				this.$http.post(baseUrl + "collect/product/list", {
					currentPage: this.page,
					pageSize: 10
				}).then(function(e) {
					wx.hideLoading();
					var a = e.data;
					var s = this;
					if (!a.success) {
						wx.showToast({
							title: a.description,
							icon: "none",
							duration: 2e3,
							mask: !0
						});
					} else {
						this.empty = false;
						s.count = a.total;
						if (0 == a.data.length && s.page > 1) {
							s.isBottom = true;
							wx.showToast({
								title: "已经到最底下了",
								icon: "none",
								duration: 1e3,
								mask: !0
							});
						} else if (a.data.length > 0 && 1 == s.page) {
							a.data.forEach(function(e) {
								e.x = 0;
								s.products.push(e);
							});
						} else if (0 == a.data.length && 1 == s.page) {
							s.empty = true;
						} else {
							a.data.forEach(function(e) {
								e.x = 0;
								s.products.push(e);
							});
						}
					}
					this.isExcute = false;
				}).catch(function(error) {
					wx.hideLoading(), console.log(error), wx.showToast({
						title: "网络错误，请检查网络",
						icon: "none",
						duration: 2e3,
						mask: !0
					});
				});
			}
			if (type === 'project') {
				this.projects = [];
				this.active = true;
				this.type = "项目";
				wx.showLoading({
					title: "加载中..."
				});
				this.$http.post(baseUrl + "collect/project/list", {
					currentPage: this.page,
					pageSize: 10,
					lat: localStorage.getItem("position.lat"),
					lang: localStorage.getItem("position.lang")
				}).then(function(e) {
					wx.hideLoading();
					var a = e.data;
					var s = this;
					if (!a.success) {
						wx.showToast({
							title: a.description,
							icon: "none",
							duration: 2e3,
							mask: !0
						});
					} else {
						this.empty = false;
						s.count = a.total;
						if (0 == a.data.length && s.page > 1) {
							s.isBottom = true;
							wx.showToast({
								title: "已经到最底下了",
								icon: "none",
								duration: 1e3,
								mask: !0
							});
						} else if (a.data.length > 0 && 1 == s.page) {
							a.data.forEach(function(e) {
								e.x = 0;
								s.projects.push(e);
							});
						} else if (0 == a.data.length && 1 == s.page) {
							s.empty = true;
						} else {
							a.data.forEach(function(e) {
								e.x = 0;
								s.projects.push(e);
							});
						}
					}
					this.isExcute = false;
				}).catch(function(error) {
					wx.hideLoading(), console.log(error), wx.showToast({
						title: "网络错误，请检查网络",
						icon: "none",
						duration: 2e3,
						mask: !0
					});
				});
			}
		},
		toProductDetail: function(item) {
			Jump.to(page.product.detail, "product=" + JSON.stringify(item));
		},
		touchStart: function(ev) {
			ev = ev || event;
			ev.preventDefault();
			//                      console.log(ev.targetTouches);
			//                      console.log(ev.changedTouches);
			if (ev.touches.length == 1) { //tounches类数组，等于1时表示此时有只有一只手指在触摸屏幕
				this.startX = ev.touches[0].clientX; // 记录开始位置
			}
		},
		touchEnd: function(ev, e) {
			ev = ev || event;
			ev.preventDefault();
			if (ev.changedTouches.length == 1) {
				var endX = ev.changedTouches[0].clientX;
				this.disX = endX - this.startX;
				if (this.disX < 0) {
					e.x = -150;
				} else if(this.disX >0) {
					e.x = 0;
				}else{
					this.toProductDetail(e);
				}
			}
		},
		deleteFav: function(item) {
			wx.showLoading({
				title: "请求中..."
			});
			var favIds = new Array();
			favIds.push(item.favId);
			this.$http.post(baseUrl + "/collect/remove", {
				ids: JSON.stringify(favIds)
			}).then(function(s){
				wx.hideLoading();
				var e = s.data;
				e.success ? (wx.showToast({
					title: "删除成功",
					icon: "none",
					duration: 2e3,
					mask: !1
				}), this.projects = [], this.products = [], this.page = 1, this.show(this.listType)) : wx.showToast({
					title: e.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			}).catch(function(error){
				wx.hideLoading(), console.log(error), wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
				
			});
		}
	},
	mounted: function() {
		window.addEventListener("scroll", this.handleScroll, {
			passive: false
		});
	},
	created: function() {
		this.picWidth = window.screen.width*2;
		this.show('project');
	}
});
